package com.bff.gaia.streaming.api.mo.gaia.io.compondedcoder;

import com.bff.gaia.streaming.api.mo.gaia.io.basiccoder.DoubleArrayCoder;

import java.io.*;
import java.util.Arrays;

public class MaxMinArrayCoder {
	Double[] max;
	Double[] min;
	byte[] data;

	public MaxMinArrayCoder(byte[] data) {
		this.data = data;
		this.makeMaxMin();
	}

	public MaxMinArrayCoder(Double[] max, Double[] min) {
		this.max = max;
		this.min = min;
		this.makeBytes();
	}

	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(bos);

		DoubleArrayCoder maxCoder = new DoubleArrayCoder(this.max);
		DoubleArrayCoder minCoder = new DoubleArrayCoder(this.min);

		byte[] maxData = maxCoder.getBytes();
		byte[] minData = minCoder.getBytes();

		try {
			dataOutputStream.writeInt(maxData.length);
			dataOutputStream.write(maxData);

			dataOutputStream.writeInt(minData.length);
			dataOutputStream.write(minData);
		} catch (IOException var8) {
			var8.printStackTrace();
		}

		this.data = bos.toByteArray();
	}

	private void makeMaxMin() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int maxLen = din.readInt();
			byte[] maxData = new byte[maxLen];
			din.readFully(maxData);
			DoubleArrayCoder maxCoder = new DoubleArrayCoder(maxData);
			this.max = maxCoder.getDoubleArray();

			int minLen = din.readInt();
			byte[] minData = new byte[minLen];
			din.readFully(minData);
			DoubleArrayCoder minCoder = new DoubleArrayCoder(minData);
			this.min = minCoder.getDoubleArray();

		} catch (IOException var8) {
			var8.printStackTrace();
		}

	}

	public Double[] getMax() {
		return this.max;
	}
	public Double[] getMin() {
		return this.min;
	}
	public byte[] getBytes() {
		return this.data;
	}
	@Override
	public String toString() {
		return "MaxMinArrayCoder{" +
			"max=" + Arrays.toString(max) +
			", min=" + Arrays.toString(min) +
			", data=" + Arrays.toString(data) +
			'}';
	}
}